<?php
//No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');

class OwnserviceModelShowexchangerate extends JModel
{
	/**
	 * Show exchange rate data array
	 *
	 * @var array
	 */
	var $_data = null;

	/**
	 * Show exchange rate total
	 *
	 * @var integer
	 */
	var $_total = null;

	/**
	 * Pagination object
	 *
	 * @var object
	 */
	var $_pagination = null;

	/**
	 * Constructor
	 *
	 * @since 1.5
	 */

	function __construct()
	{
		parent::__construct();

		global $mainframe,$option;

		$limit		= $mainframe->getUserStateFromRequest( $option.'.showexchangerate.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
		$limitstart = $mainframe->getUserStateFromRequest( $option.'.showexchangerate.limitstart', 'limitstart', 0, 'int' );
		
		$limitstart = ( $limit != 0 ? (floor($limitstart / $limit) * $limit) : 0 );

		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);
	}
	
	/**
	 * Method to get Exchange Rate data
	 *
	 * @access public
	 * @return array
	 */
	function getData() {

		// Lets load the exchange rate if it doesn't already exist
		if (empty($this->_data))
		{
			$query = $this->_buildQuery();
			$this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
			//echo $this->_db->_sql;
		}

		return $this->_data;
	}

	/**
	 * Method to get the total number of exchange rate
	 *
	 * @access public
	 * @return integer
	 */
	function getTotal() {
		// Lets load the exchange rate if it doesn't already exist
		if (empty($this->_total))
		{
			$query = $this->_buildQuery();
			$this->_total = $this->_getListCount($query);
		}

		return $this->_total;
	}

	/**
	 * Method to get a pagination object for the exchange rate
	 *
	 * @access public
	 * @return object
	 */
	function getPagination() {

		// Lets load the customer account if it doesn't already exist
		if (empty($this->_pagination)) {
			jimport('joomla.html.pagination');
			$this->_pagination = new FMCPagination( $this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
		}

		return $this->_pagination;
	}

	/**
	 * Method to build the query for the accounts
	 *
	 * @access private
	 * @return integer
	 * @since 1.0
	*/
	function _buildQuery()
	{
		$user 	=& JFactory::getUser();
		$aid 	= $user->get('aid', 0);

		// Get the WHERE and ORDER BY clauses for the query
		$where		= $this->_buildContentWhere();
		$orderby	= $this->_buildContentOrderBy();

		//Query to retrieve all exchange rate that belong under the web links section and that are published.
		$query = ' SELECT r.*, c.name, c.code '
				.' FROM #__ownservice_exchangerates AS r'
				.' LEFT JOIN #__ownservice_currencies AS c ON c.id=r.currency_id'
				.$where
				.' GROUP BY r.id '
				.$orderby;

		return $query;
	}

	/**
	 * Method to build the orderby clause of the query for the show exchange rate
	 *
	 * @access private
	 * @return string
	 * @since 1.0
	 */
	function _buildContentOrderBy(){
		global $mainframe, $option;

		$filter_order		= $mainframe->getUserStateFromRequest($option.'.showexchangerate.filter_order', 'filter_order', 'r.id', 'cmd');
		$filter_order_Dir	= $mainframe->getUserStateFromRequest($option.'.showexchangerate.filter_order_Dir', 'filter_order_Dir', '', 'cmd');

		$orderby 	= ' ORDER BY '.$filter_order.' '.$filter_order_Dir;

		return $orderby;
	}

	/**
	 * Method to build the where clause of the query for the show exchange rates
	 *
	 * @access private
	 * @return string
	 * @since 1.0
	 */
	function _buildContentWhere()
	{
		global $mainframe, $option;

		//Initialise variable
		$user 		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();
		$branch_id	= $ownuser->get('branch_id', 0);

		$search 		= $mainframe->getUserStateFromRequest($option.'.showexchangerate.fields.search', 'search', '', 'string' );
		$search 		= $this->_db->getEscaped( trim(JString::strtolower( $search ) ) );

		$where = array();
		
		//$where[] = 'r.deleted = 0';
		if($branch_id){
			$where[] = 'r.branch_id='.$branch_id;
		}
		
		if ($search) {
			$where[] = '(LOWER( c.name ) LIKE '.$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false ) .
				' OR LOWER( c.code ) LIKE '.$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false ) .
				' OR LOWER( c.sign ) LIKE '.$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false );
		}

		$where 		= ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );

		return $where;
	}
}